<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Cat;
use App\Stadium;
use App\Detail;
use App\Order;
use App\User;

class IndexController extends Controller
{
    public $cats;
    protected $middleware = ['App\Http\Middleware\AdminMiddleware'=>[]];

    public function __construct(){
        $this->cats = Cat::get()->toArray();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.index');
    }

    public function catlist(){
        //$cats = DB::table('cats')->get();
        $cats = $this->getTree();
        return view('admin.cat_list', ['cats'=>$cats]);
    }

    public function catadd(){
        $cats = Cat::where('pid', 0)->get();
        return view('admin.cat_add', ['cats'=>$cats]);
    }


    //栏目删除
    public function cat_del($pid, $id){
        if($pid == 0){
            return '栏目为父栏目，无法删除';
        }
        return Cat::destroy($id) ? 'success' : 'fail';
    }
    //栏目修改
    public function cat_edit($pid,$id){
        $cats = new Cat();
        $cat = $cats->find($id);
        $parent = $cats->where('pid', 0)->get();
        return view('admin.cat_edit', ['cat'=>$cat, 'parent'=>$parent]);
    }

    public function post_edit(Request $req){
        $cats = Cat::find($req->cat_id);
        $cats->cat_name = $req->cat_name;
        $cats->pid = $req->pid;
        return $cats->save() ? 'success' : 'fail';
    }


    public function catpost(Request $req){
        if(Cat::where('cat_name', $req->cat_name)->where('pid', $req->cat_id)->first()){
            echo '插入失败，栏目已存在';
            exit();
        }
        $cats = new Cat();
        $cats->cat_name = $req->cat_name;
        $cats->pid = $req->cat_id;
        return $cats->save()? 'success' : 'fail';
    }


    public function getTree($pid = 0, $lev = 0){
        $tree = array();
        foreach($this->cats as $v){
            if($v['pid'] == $pid){
                $v['lev'] = $lev;
                $tree[] = $v;
                $tree = array_merge($tree, $this->getTree($v['cat_id'], $lev+1));
            }
        }
        return $tree;
    }

    //场馆添加
    public function stadiumadd(){
        $cats = Cat::where('pid', 0)->get();
        return view('admin.stadium_add', ['cats'=>$cats]);
    }

    //场馆添加提交
    public function stadiumpost(Request $req){
        $stadiums = new Stadium();
        //dd($stadiums);exit;
        if ($req->file()) {
            $path = 'upload/'.date('Y/md/');
            // dd(date('Y/md/'));
            // Storage
            $filename1 = date('Ymd').mt_rand(100000,999999).$req->file('image1')->getClientOriginalName();
            $filename2 = date('Ymd').mt_rand(100000,999999).$req->file('image2')->getClientOriginalName();

            if ( !($req->file('image1')->isValid() && $req->file('image2')->isValid()) ) {
                return '文件上传失败';
            }

            $req->file('image1')->move($path, $filename1);
            $req->file('image2')->move($path, $filename2);
        }
        // dd( $req->file('image1')->move($path, $filename1) );
        $stadiums->stadiumname = $req->stadiumname;
        $stadiums->address = $req->address;
        $stadiums->price = $req->price;
        $stadiums->bus = $req->bus;
        $stadiums->subway = $req->subway;
        $stadiums->service = $req->service;
        $stadiums->park = $req->park;
        $stadiums->sale = $req->sale;
        $stadiums->images = $path.$filename1.','.$path.$filename2;

        if($stadiums->save()){
            return '添加成功';
        }
    }

    //场馆列表
    public function stadiumlist(){
        $stadiums = Stadium::get();
        return view('admin.stadium', ['stadiums'=>$stadiums]);
    }

    //场馆详情
    public function st_detail($stadium_id, $name){
        return view('admin.stadium_detail', ['stadium_id'=>$stadium_id, 'name'=>$name]);
    }

    //场馆详情提交
    public function postdetail(Request $req){
        $detail = new Detail;
        //dd($req);
        $time = substr($req->time, 0, 2);
        //dd($req);
        $interval = substr($req->endtime, 0, 2) - $time;
        //判断是否已添加该日期
        if ( !$detail->where('date', $req->date)->where('stadium_id', $req->stadium_id)->get()->isEmpty() ) {
            return '该日期场馆信息已添加';
        }
        if($interval <0){
            return '时间格式有误';
        } elseif ($interval > 16) {
            return '时间间隔不能超过16小时';
        }

            for($num = 1; $num<=$req->number; $num++){
                for($t=0; $t<$interval; $t++){
                    $data = [];
                    $data['date'] = $req->date;
                    $data['time'] = ($time+$t).':00';
                    $data['number'] = $num;
                    $data['price'] = $req->price;
                    $data['status'] = $req->status;
                    $data['stadium_id'] = $req->stadium_id;
                    //print_r($data);
                    $detail->create($data);
                }
            }
            return 'success';
    }

    public function order(){
        $orders = Order::get();
        return view('admin.order', ['orders'=>$orders]);
    }

    //进场验证
    public function order_del(Request $req){
        $order = new Order();
         if($info = $order->where('validate', $req->validate)->first()){
            return $order->where('order_id', $info->order_id)->update(['status'=>2]) ? 'success' : 'fail';
        }
    }


    //用户列表
    public function user(){
        $users = User::get();
        //dd($users);
        return view('admin.user', ['users'=>$users]);
    }
}
